<?php
/*******************************************************************************
reportDisplayer.class.php


Created by Jason White (jbwhite@emory.edu)
Modified by NCSU Libraries, NC State University. Modifications by Troy Hurteau (libraries.opensource@ncsu.edu).

This file is part of NCSU's distribution of ReservesDirect. This version has not been downloaded from Emory University
or the original developers of ReservesDirect. Neither Emory University nor the original developers of ReservesDirect have authorized
or otherwise endorsed or approved this distribution of the software.

Copyright (c) 2004-2006 Emory University, Atlanta, Georgia.

Licensed under the NCSU ReservesDirect License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. You may obtain a copy of the full License at
 http://www.lib.ncsu.edu/it/opensource/

ReservesDirect is distributed in the hope that it will be useful,
but is distributed "AS IS" and WITHOUT ANY WARRANTY, without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE, and without any warranty as to non-infringement of any third
party's rights. See the License for the specific language governing permissions and limitations under the License.

The original version of ReservesDirect is located at:
http://www.reservesdirect.org/

This version of ReservesDirect, distributed by NCSU, is located at:
http://code.google.com/p/reservesdirect-ncsu/

*******************************************************************************/
require_once(APPLICATION_PATH . '/classes/terms.class.php');
require_once(APPLICATION_PATH . '/classes/permissions.class.php');
require_once(APPLICATION_PATH . '/managers/ajaxManager.class.php');

class reportDisplayer extends baseDisplayer {
	/**
	 * Display List all reports available to user
	 *
	 * $reportList array reportID, reportTitle
	 */
	
	function displayReportList($reportList)
	{
?>		
<table width="60%" border="0" cellspacing="0" cellpadding="0" align="center">
	<tr><td width="100%"><img src="images/spacer.gif" width="1" height="5"></td></tr>
<?php 
		if (empty($reportList)) {
?>
	<tr><td>No Reports Specified</td></tr>
<?php 		} else { 
			$i = 0;
			while($i<count($reportList))
			{
?>
	<tr>
		<td NOWRAP><a href="index.php?cmd=viewReport&reportID=<?php print($reportList[$i]['report_id']); ?>"><?php print($reportList[$i]['title']); ?></a></td>			
	<tr>
<?php 			$i++;
			}
		}
?>
</table>
<?php 
	}
	
	function displayReport($title, &$dataSet)
	{
		self::generateTsvLink($dataSet);
?>
	<div style="align:right; text-align:right; padding=5px;"><a href="index.php?cmd=reportsTab">Return to Reports List</a></div>
	<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
		<tr><td width="100%"><img src="images/spacer.gif" width="1" height="5"></td></tr>
		<tr><td>&nbsp;</td></tr>
	    <tr>
	    	<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
	    		<tr align="left" valign="top">
	    			<td class="headingCell1"><div align="center"><?php print($title);?></div></td>
	    			<td width="75%">&nbsp;</td>
	    		</tr>
	    		</table>
	    	</td>
	    </tr>
	    <tr>
	    	<td align="left" valign="top" class="borders">
	    		<table width="100%" border="0" cellpadding="2" cellspacing="0" class="displayList">
<?php 
	    if (!empty ($dataSet))
	    {
?>
					<tr>
<?php 	    foreach ($dataSet[0] as $key => $value) {
?> 
						<td><b><?php print($key); ?></b></td>
<?php 		} ?>
					</tr>	    	
<?php 		    
			$i = 0;
			$dataSet_0 = '';  //when first column changes we will insert a blank row
			while($i<count($dataSet))
			{
				$rowClass = ($i % 2) ? 'evenRow' : 'oddRow';
?>				
					<tr align="left" valign="middle" class="<?php  print($rowClass); ?>">
<?php 			foreach ($dataSet[$i] as $key => $value) {
					$value = urldecode($value);
?>
						<td><?php print($value); ?></td>
<?php 				
				}
				$i++;
?>
				</tr>
<?php 				
			}
	    } else {
?>
				<tr><td><font color="red">Report completed with no results</font></td></tr>
<?php   } ?>
			</table>
		</td>
	</tr>
</table>
<?php 
	}
	
	static function generateTsvLink($data){
?>
		<div style="text-align:center;">
			<form method="post" action="tsvGenerator.php"><br/>
				<input type="hidden" name="dataSet" value="<?php print(base64_encode(serialize($data))); ?>"/>
	    		<input type="submit" name="exportTsv" value="Export to Spreadsheet"/>
	    	</form>
	    </div>
<?php 
	}
	
	function enterReportParams(&$report)
	{
		global $u, $g_permission;
?>
	<div style="align:right; text-align:right; padding=5px;"><a href="index.php?cmd=reportsTab">Return to Reports List</a></div>	
		<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
			<tr>
	    		<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
	    			<tr align="left" valign="top">
	    				<td class="headingCell1"><div align="center"><?php  print($report->getTitle()); ?></div></td>
	    				<td width="75%">&nbsp;</td>
	    	        </tr>
	    		</table></td>
	    	</tr>
<?php 
	    switch ($report->getParam_group())
	    {
	    	case 'term_lib': 
	    		self::showTermLibraryForm($report,$u);
	    	break;
	    	
	    	case 'term':
				self::showTermForm($report);
			break;
			
	    	case 'term_dates':
	    		//get array of all terms
				self::showTermDateForm($report);
	    	break;
	    	
	    	case 'term_dates_usertype':
	    		//get array of all terms
				self::showTermDateUserTypeForm($report);
	    	break;
		
			case 'fyear_dates':
				self::showFiscalYearPicker($report);
			break;
			case 'term_usertype':
				self::showTermUserTypeForm($report);
			break;	
			case 'term_itemgroup':
				self::showTermItemGroupForm($report);
			break;    	
	    	
			case 'class':
				//generate class list for instructors
				$class_list = $u->getCourseInstancesToEdit($report);				
?>
	<tr>
		<td colspan="2" valign="top">
		
			<?php self::displaySelectClass('viewReport', $class_list, '', array('reportID'=>$_REQUEST['reportID'])); ?>

		</td>
	</tr>
<?php
			break;			
			
			default:
				self::showReportConfigError($report->getParam_group());
	    }
	    echo "</table>";
	}
	
	static function showTermLibraryForm($report,$u)
	{
		$t = new terms();
?>
	<tr>
		<td colspan="2" valign="top">
			<form method="post" action="index.php">
			
				<input type="hidden" name="reportID" value="<?php print($report->getReportID()); ?>" />
				<input type="hidden" name="cmd" value="viewReport" />
				
				(Use SHIFT/CTRL keys to select multiple values)
				<br/>
				<strong>Term(s):</strong>
				<br/>
				<select multiple="true" name="term_id[]" size="5">
<?php		foreach($t->getTerms(true) as $term): ?>
					<option value="<?php print($term->getTermID()); ?>"><?php print($term->getTerm()); ?></option>
<?php		endforeach; ?>
				</select>
				
				<br/>
				
				<strong>Library(ies):</strong>
				<br/> 
				<select multiple="true" name="library_id[]" size="5">
<?php		foreach($u->getLibraries() as $lib): ?>
					<option value="<?php print($lib->getLibraryID()); ?>"><?php print($lib->getLibrary()); ?></option>
<?php		endforeach; ?>
				</select>
				<br/>
				<input type="submit" name="submit" value="Generate Report" />
			</form>
		</td>
	</tr>
<?php		
	}
	
	static function showTermForm($report)
	{
	    		$t = new terms();
?>
	<tr>
		<td colspan="2" valign="top">
			<form method="post" action="index.php">
			
				<input type="hidden" name="reportID" value="<?php print($report->getReportID()); ?>" />
				<input type="hidden" name="cmd" value="viewReport" />
				
				(Use SHIFT/CTRL keys to select multiple values)
				<br/>
				<strong>Term(s):</strong>
				<br/>
				<select multiple="true" name="term_id[]" size="5">
<?php foreach($t->getTerms(true) as $term){ ?>
					<option value="<?php print($term->getTermID()); ?>"><?php print($term->getTerm()); ?></option>
<?php } ?>
				</select>

				<br/>
				<input type="submit" name="submit" value="Generate Report" />
			</form>
		</td>
	</tr>
<?php    				
	}
	
	static function showTermUserTypeForm($report)
	{
	    		$t = new terms();
?>
	<tr>
		<td colspan="2" valign="top">
			<form method="post" action="index.php">
			
				<input type="hidden" name="reportID" value="<?php print($report->getReportID()); ?>" />
				<input type="hidden" name="cmd" value="viewReport" />
				
				(Use SHIFT/CTRL keys to select multiple values)
				<br/>
				<strong>Term(s):</strong>
				<br/>
				<select multiple="true" name="term_id[]" size="5">
<?php		foreach($t->getTerms(true) as $term): ?>
					<option value="<?php print($term->getTermID()); ?>"><?php print($term->getTerm()); ?></option>
<?php		endforeach; ?>
				</select>
				<strong>User Groups(s):</strong>
				<br/>
				<select multiple="true" name="permission_id[]" size="5">
<?php		foreach(permissions::getAllIds() as $permissionId){ ?>
					<option selected="selected" value="<?php print($permissionId); ?>"><?php print(permissions::getLabel($permissionId)); ?></option>
<?php		} ?>
				</select>

				<br/>
				<input type="submit" name="submit" value="Generate Report" />
			</form>
		</td>
	</tr>
<?php    			
	}
	
	static function showFiscalYearPicker($report)
	{
		$year = (int)date('Y');
?>
	<tr>
		<td colspan="2" valign="top">
			<script type="text/javascript">
				jQuery(document).ready(function(){
					jQuery('#fYearPicker').keyup(function(){
						var newYear = jQuery(this).val();
						if(newYear.length == 4){
							jQuery('#fYearStart').val( (parseInt(newYear) - 1 )+ '-07-01');
							jQuery('#fYearEnd').val( newYear + '-06-30');
						}
					});
				});
			</script>
			
			<form method="post" action="index.php">
				<strong>Fiscal Year:</strong>
				<br/>
				<input type="text" id="fYearPicker" name="fYear" value="<?php print($year); ?>" />
				<input type="hidden" name="reportID" value="<?php print($report->getReportID()); ?>" />
				<input type="hidden" name="cmd" value="viewReport" />
				
				<br/>
				<strong>Dates (required):</strong>
				<br/>
				<input type="text" id="fYearStart" name="begin_date" size="10" maxlength="10" value="<?php print(($year - 1) . '-07-01') ?>" />&nbsp;to&nbsp;<input type="text" id="fYearEnd" name="end_date" size="10" maxlength="10" value="<?php print($year.'-06-30'); ?>" />&nbsp; (YYYY-MM-DD)
				<br/>
				<input type="submit" name="submit" value="Generate Report" />
			</form>
		</td>
	</tr>
<?php
	}

	static function showTermItemGroupForm($report)
	{
	    		$t = new terms();
?>
	<tr>
		<td colspan="2" valign="top">
			<form method="post" action="index.php">
			
				<input type="hidden" name="reportID" value="<?php print($report->getReportID()); ?>" />
				<input type="hidden" name="cmd" value="viewReport" />
				
				(Use SHIFT/CTRL keys to select multiple values)
				<br/>
				<strong>Term(s):</strong>
				<br/>
				<select multiple="true" name="term_id[]" size="5">
<?php		foreach($t->getTerms(true) as $term): ?>
					<option value="<?php print($term->getTermID()); ?>"><?php print($term->getTerm()); ?></option>
<?php		endforeach; ?>
				</select>
				<strong>Item Groups(s):</strong>
				<br/>
				<select multiple="true" name="item_group[]" size="5">
					<option selected="selected" value="MONOGRAPH">Monograph</option>
					<option selected="selected" value="MULTIMEDIA">Multimedia</option>
					<option selected="selected" value="ELECTRONIC">Electronic</option>
					<option selected="selected" value="VIDEO">Video</option>
				</select>

				<br/>
				<input type="submit" name="submit" value="Generate Report" />
			</form>
		</td>
	</tr>
<?php    			
	}
	
	static function showTermDateForm($report)
	{
		$t = new terms();
		$terms = $t->getTerms(true);
?>
	<tr>
		<td colspan="2" valign="top">
<?php  self::showTermDateScript($terms); ?>
			
			<form method="post" action="index.php">
				<input type="hidden" name="reportID" value="<?php print($report->getReportID()); ?>" />
				<input type="hidden" name="cmd" value="viewReport" />
				
				<br/>
<?php  
				self::showTermDatePicker($terms);
?>
				<br/>
				<input type="submit" name="submit" value="Generate Report" />
			</form>
		</td>
	</tr>
<?php 	    				
	}
	
	static function showTermDatePicker($terms)
	{
?>
<strong>Term:</strong>
				<br/>
				<select name="term_id" onchange="javascript: prefill_term_dates(this.options[this.selectedIndex].value);">
<?php		foreach($terms as $term): ?>
					<option value="<?php print($term->getTermID()); ?>"><?php print($term->getTerm()); ?></option>
<?php		endforeach; ?>
				</select>
				<br/>
				<strong>Dates (required):</strong>
				<br/>
				<input type="text" id="begin_date" name="begin_date" size="10" maxlength="10" value="<?php echo $terms[0]->getBeginDate(); ?>" />&nbsp;to&nbsp;<input type="text" id="end_date" name="end_date" size="10" maxlength="10" value="<?php echo $terms[0]->getEndDate(); ?>" />&nbsp; (YYYY-MM-DD)
<?php 		
	}
	
	static function showTermDateScript($terms)
	{
?>
		<script type="text/javascript">
				//define associative arrays indexed by term_id, containing begin and end dates
				var term_begin_dates = {
<?php
	//need this so that we can string the rest w/ preceding comma
		echo '"null":"null"';
		foreach($terms as $term) {
			echo ', "'.$term->getTermID().'":"'.$term->getBeginDate().'"';
		}
?>				
				};
				var term_end_dates = {
<?php
			//need this so that we can string the rest w/ preceding comma
			echo '"null":"null"';
			foreach($terms as $term) {
				echo ', "'.$term->getTermID().'":"'.$term->getEndDate().'"';
			}
?>				
				};
				
				function prefill_term_dates(term_id) {					
					if(document.getElementById('begin_date')) {
						document.getElementById('begin_date').value = term_begin_dates[term_id];
					}
					if(document.getElementById('end_date')) {
						document.getElementById('end_date').value = term_end_dates[term_id];
					}
				}
			</script>
<?php 
	}
	
	static function showTermDateUserTypeForm($report)
	{
		$t = new terms();
		$terms = $t->getTerms(true);
?>
	<tr>
		<td colspan="2" valign="top">
<?php  self::showTermDateScript($terms); ?>
			
			<form method="post" action="index.php">
				<input type="hidden" name="reportID" value="<?php print($report->getReportID()); ?>" />
				<input type="hidden" name="cmd" value="viewReport" />
				
				<br/>
<?php  
				self::showTermDatePicker($terms);
?>
				<br/>
				<select multiple="true" name="permission_id[]" size="5">
<?php		foreach(permissions::getAllIds() as $permissionId){ ?>
					<option selected="selected" value="<?php print($permissionId); ?>"><?php print(permissions::getLabel($permissionId)); ?></option>
<?php		} ?>
				</select>
				<br/>
				<input type="submit" name="submit" value="Generate Report" />
			</form>
		</td>
	</tr>
<?php    	
	}
	
	static function showReportConfigError($param_group)
	{
?>
		<p>This report is not configured properly. Invalid parameter group: "<?php print($param_group); ?>"</p>
<?php 		
	}
}


